package com.imageim.yimirror.common.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.imageim.yimirror.common.util.UIUtils;

import java.util.List;

/**
 * Created by yuaih on 2018/5/22.
 */
public class SpreadView extends View {
    private static final String TAG = "SpreadView";

    private Paint paint;

    private List<String> mTvList;
    private float centerX;
    private float centerY;
    //偏移量
    private float offSet;
    //偏移量比率
    private float offScale;
    //根据最大字体大小缩放比例
    private float fontScaleSize;
    //最大字体大小
    private float maxFontSize = UIUtils.dip2px(getContext(), 60);


    //左中心View的宽高
    private float mLeftCentetViewWidth, mLeftCenterViewHeight, mLeftCenterViewX, mLeftCenterViewY;
    //作为标准的右边View的宽高
    private float mRightCenterViewWidth, mRightCenterViewHeight, mRightCenterViewX, mRightCenterViewY;
    //左上中心点View
    private float mLeftTopCenterViewX, mLeftTopCenterViewY, mLeftTopCenterWidth, mLeftTopCenterHeight;

    /**
     * 右上中心点View
     **/
    private float mRightTopCenterViewX, mRightTopCenterViewY, mRightTopCenterWidth, mRightTopCenterHeight;


    /**
     * 中心点View宽高
     **/
    private float centerViewHeight = 0f;
    private float centerViewWidth = 0f;
    private String drawText;
    private int mHeightSpec;
    private int mWidthSpec;

    public SpreadView(Context context) {
        super(context);
        init();
    }

    public SpreadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SpreadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setDither(true);
        paint.setColor(Color.parseColor("#c9ff0045"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //TextView 区域宽高
        if (widthSize <= 0) {
            widthSize = getContext().getResources().getDisplayMetrics().widthPixels;
        }
        if (heightSize <= 0) {
            heightSize = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
        }
        mWidthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        mHeightSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        setMeasuredDimension(mWidthSpec, mHeightSpec);

        //字体缩放比率

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        centerX = getWidth() / 2.0f;
        centerY = getHeight() / 2.0f;

        fontScaleSize = getHeight() / 300.0f;
        maxFontSize = maxFontSize * fontScaleSize;
        //偏移比率
        offScale = getHeight() / 300.0f;
        //        offSet = 30 * offScale;
        paint.setTextSize(maxFontSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mTvList != null && mTvList.size() > 0) {
            for (int i = 0; i < mTvList.size(); i++) {
                if (!TextUtils.isEmpty(mTvList.get(i))) {
                    drawText = mTvList.get(i);
                    //中心点右边View的中心点计算需要 / 2
                    switch (i) {
                        case 0:
                            paint.setColor(Color.parseColor("#c9ff0045"));
                            paint.setAlpha(255);
                            canvas.drawText(drawText, centerX - getTextWidth(drawText) / 2.0f, centerY + getTextHeight(drawText) / 2.0f, paint);
                            centerViewHeight = getTextHeight(mTvList.get(0));
                            centerViewWidth = getTextWidth(mTvList.get(0));
                            break;
                        case 1:
                            paint.setColor(Color.WHITE);
                            paint.setAlpha(220);
                            paint.setTextSize(maxFontSize * 0.7f);
                            canvas.drawText(drawText, centerX - getTextWidth(drawText) / 2.0f + offScale * 60, centerY - centerViewHeight / 2.0f - offScale * 60, paint);
                            break;
                        case 2:
                            canvas.drawText(drawText, centerX - getTextWidth(drawText) / 2.0f + offScale * 170, centerY + centerViewHeight + offScale * 70 >= mHeightSpec ? mHeightSpec : centerY + centerViewHeight + offScale * 70, paint);
                            break;
                        case 3:
                            paint.setTextSize(maxFontSize * 0.6f);
                            paint.setAlpha(180);
                            canvas.drawText(drawText, centerX - centerViewWidth / 2 - getTextWidth(drawText) - offScale * 80, centerY - getTextHeight(drawText) / 2.0f - offScale * 50, paint);
                            //设置为左边View的中心View
                            mLeftCenterViewX = centerX - centerViewWidth / 2 - getTextWidth(drawText) - offScale * 80;
                            mLeftCenterViewY = centerY - getTextHeight(drawText) / 2.0f - offScale * 50;
                            mLeftCentetViewWidth = getTextWidth(drawText);
                            mLeftCenterViewHeight = getTextHeight(drawText);
                            break;
                        case 4:
                            paint.setTextSize(maxFontSize * 0.7f);
                            paint.setAlpha(130);
                            canvas.drawText(drawText, centerX + centerViewWidth / 2 + getTextWidth(drawText) / 2 + offScale * 150, centerY - getTextHeight(drawText) / 2, paint);
                            //设置为右边View的中心View
                            mRightCenterViewX = centerX + centerViewWidth / 2 + getTextWidth(drawText) / 2 + offScale * 150;
                            mRightCenterViewY = centerY - getTextHeight(drawText) / 2;
                            mRightCenterViewWidth = getTextWidth(drawText);
                            mRightCenterViewHeight = getTextHeight(drawText);
                            break;
                        case 5:
                            paint.setTextSize(maxFontSize * 0.5f);
                            paint.setAlpha(160);
                            canvas.drawText(drawText, mLeftCenterViewX - getTextWidth(drawText) - offScale * 100 <= 0 ? 0 : mLeftCenterViewX - getTextWidth(drawText) - offScale * 10, mLeftCenterViewY - mLeftCenterViewHeight / 2 + offScale * 100, paint);
                            //左上5号位置View设置为左上中心View
                            mLeftTopCenterViewY = mLeftCenterViewY - mLeftCenterViewHeight / 2 + offScale * 100;
                            mLeftTopCenterWidth = getTextWidth(drawText);
                            mLeftTopCenterHeight = getTextHeight(drawText);
                            break;
                        case 6:
                            paint.setTextSize(maxFontSize * 0.7f);
                            canvas.drawText(drawText, mLeftCenterViewX - getTextWidth(drawText) / 2 - offScale * 150 <= 0 ? 0 : mLeftCenterViewX - getTextWidth(drawText) / 2 + offScale * 50, mLeftCenterViewY + mLeftCenterViewHeight / 2 + offScale * 160, paint);
                            //将6号位置设置为左下中心View
                            mLeftCenterViewY = mLeftCenterViewY + mLeftCenterViewHeight / 2 + offScale * 50;
                            mLeftCentetViewWidth = getTextWidth(drawText);
                            mLeftCenterViewHeight = getTextHeight(drawText);
                            break;
                        case 7:
                            paint.setAlpha(100);
                            canvas.drawText(drawText, mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 50 >= mWidthSpec ? mWidthSpec : mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 30, mRightCenterViewY - mRightCenterViewHeight / 2 - offScale * 50, paint);
                            mRightTopCenterViewY = mRightCenterViewY - mRightCenterViewHeight / 2 - offScale * 30;
                            mRightTopCenterWidth = getTextWidth(drawText);
                            mRightTopCenterHeight = getTextHeight(drawText);
                            break;
                        case 8:
                            paint.setAlpha(200);
                            paint.setTextSize(maxFontSize * 0.45f);
                            canvas.drawText(drawText, mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 60 >= mWidthSpec ? mWidthSpec : mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 60, mRightCenterViewY + mRightCenterViewHeight / 2 + offScale * 80, paint);
                            mRightCenterViewY = mRightCenterViewY + mRightCenterViewHeight / 2 + offScale * 80;
                            mRightCenterViewWidth = getTextWidth(drawText);
                            mRightCenterViewHeight = getTextHeight(drawText);
                            break;
                        //                        case 9:
                        //                            paint.setTextSize(maxFontSize * 0.35f);
                        //                            paint.setAlpha(190);
                        //                            canvas.drawText(drawText, mLeftCenterViewX + getTextWidth(drawText) - offScale * 105, mLeftCenterViewY + mLeftCenterViewHeight + offScale * 40 >= mHeightSpec ? mHeightSpec : mLeftCenterViewY + mLeftCenterViewHeight / 2 + offScale * 40, paint);
                        //                            mLeftCenterViewY = mLeftCenterViewY + mLeftCenterViewHeight + offScale * 40 >= mHeightSpec ? mHeightSpec : mLeftCenterViewY + mLeftCenterViewHeight / 2 + offScale * 40;
                        //                            mLeftCentetViewWidth = getTextWidth(drawText);
                        //                            mLeftCenterViewHeight = getTextHeight(drawText);
                        //                            break;
                        //                        case 10:
                        //                            paint.setTextSize(maxFontSize * 0.35f);
                        //                            paint.setAlpha(190);
                        //                            canvas.drawText(drawText, mLeftCenterViewX + getTextWidth(drawText) / 2 - offScale * 75, mLeftTopCenterViewY - mLeftCenterViewHeight / 2 - offScale * 30 <= 0 ? 0 : mLeftTopCenterViewY - mLeftCenterViewHeight / 2 - offScale * 30, paint);
                        //                            mLeftTopCenterViewY = mLeftTopCenterViewY - mLeftCenterViewHeight / 2 - offScale * 30 <= 0 ? 0 : mLeftTopCenterViewY - mLeftCenterViewHeight / 2 - offScale * 30;
                        //                            mLeftTopCenterWidth = getTextWidth(drawText);
                        //                            mLeftTopCenterHeight = getTextHeight(drawText);
                        //                            break;
                        //                        case 11:
                        //                            paint.setTextSize(maxFontSize * 0.4f);
                        //                            paint.setAlpha(190);
                        //                            canvas.drawText(drawText, mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 35 >= mWidthSpec ? mWidthSpec : mRightCenterViewX + getTextWidth(drawText) / 2 + offScale * 35, mRightCenterViewY + mRightCenterViewHeight / 2 + offScale * 60, paint);
                        //                            mRightCenterViewY = mRightCenterViewY + mRightCenterViewHeight / 2 + offScale * 60;
                        //                            break;
                        //                        case 12:
                        //                            canvas.drawText(drawText, mRightCenterViewX + getTextWidth(drawText) / 2 - offScale * 60 >= mWidthSpec ? mWidthSpec : mRightCenterViewX + getTextWidth(drawText) / 2 - offScale * 60, mRightTopCenterViewY - mRightTopCenterHeight / 2 - offScale * 30 >= mHeightSpec ? mHeightSpec : mRightTopCenterViewY - mRightTopCenterHeight / 2 - offScale * 30, paint);
                        //                            mRightTopCenterViewY = mRightCenterViewY - mRightCenterViewHeight - offScale * 50 - getTextHeight(drawText) / 2;
                        //                            break;
                        default:
                    }
                }
            }

        }
    }


    public void setTvList(List<String> tvList) {
        this.mTvList = tvList;
        invalidate();
    }

    /**
     * 偏移量比率
     *
     * @param offSetScale float
     */
    public void setOffsetScale(float offSetScale) {
        this.offScale = offSetScale;
    }

    /**
     * 字体的缩放比率
     *
     * @param fontScaleSize float
     */
    public void fontScale(float fontScaleSize) {
        this.fontScaleSize = fontScaleSize;
    }


    /**
     * 设置画笔颜色
     *
     * @param color 十六进制Color
     */
    public void setPaintColor(String color) {
        paint.setColor(Color.parseColor(color));
    }

    /**
     * 字体宽度
     *
     * @param text
     * @return
     */
    private float getTextWidth(String text) {
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        Rect bound = new Rect();
        paint.getTextBounds(text, 0, text.length(), bound);
        return bound.width();
    }

    /**
     * 字体高度
     *
     * @param text
     * @return
     */
    private float getTextHeight(String text) {
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        Rect bound = new Rect();
        paint.getTextBounds(text, 0, text.length(), bound);
        return bound.height();
    }
}